10 TEXT : HOME : HTAB 12: INVERSE : PRINT "WORD UNSCRAMBLER": DIM A$(720): NORMAL
20 VTAB 6: INPUT "GIMME THE LETTERS! ";O$
30 C = 1:L = LEN(O$):Q = 1: FOR S = 1 TO L:Q = Q *S: NEXT S: REM C IS A COUNTER; Q CALCULATES L! (L FACTORIAL), THE TOTAL NUMBER OF PERMUTATIONS
40 FLASH : VTAB 12: HTAB 16: PRINT "THINKING": NORMAL
50 IF L = 4 THEN FOUR$ = O$: GOSUB 330: GOTO 160
60 IF L = 5 THEN FIVE$ = O$: GOSUB 420: GOTO 160
64 REM
65 REM **6-LETTER LOOP**
66 REM
70 SIX$ = O$
80 FOR A = 0 TO 5
90 Y$ = LEFT$(SIX$,1):FIVE$ = RIGHT$(SIX$,5): REM PULL THE FIRST LETTER OFF
100 GOSUB 420: REM SEND WHAT'S LEFT TO THE 5-LETTER LOOP
110 FOR P = 1 +120 *A TO 120 +120 *A: REM A COUNTS THE NUMBER OF TIMES THROUGH THE 6-LETTER LOOP; EACH TIME THROUGH THERE ARE 120 NEW COMBINATIONS TO BE ADDED TO THE ARRAY A$()
120 A$(P) = Y$ +A$(P)
130 NEXT P: REM THIS LOOP TAKES 120 FIVE-LETTER COMBINATIONS FROM THE 5-LETTER LOOP AND ADDS THE SIXTH LETTER PULLED OFF IN LINE 90 TO MAKE 6-LETTER COMBINATIONS
140 SIX$ = RIGHT$(SIX$,1) + LEFT$(SIX$,5): REM TAKE THE LAST LETTER OF THE WORD AND STICK IT ON THE FRONT INSTEAD - THIS MAKES A NEW 6-LETTER COMBINATION
150 NEXT A: REM DO IT ALL AGAIN WITH THE NEW 6-LETTER COMBINATION
154 REM
155 REM **OUTPUT ROUTINE**
156 REM
160 HOME : FOR Y = 1 TO Q
170 PRINT A$(Y),: IF Y/60 = INT(Y/60) THEN GOSUB 300
180 NEXT Y
190 PRINT
200 INPUT "ANOTHER ONE? (Y/N) ";V$
210 IF V$ < >"Y" THEN END
220 RUN
224 REM
225 REM **3-LETTER LOOP**
226 REM
230 E$ = THREE$
240 FOR I = 1 TO 3
250 GOSUB 280:A$(C) = E$:C = C +1: GOSUB 280: GOSUB 290:A$(C) = E$:C = C +1
260 NEXT I: REM THIS IS THE HEART OF THE PROGRAM. LINE 280 "SWAPS" THE 3-LETTER WORD (ABC BECOMES ACB); THE RESULT GOES IN ARRAY A$() WHICH STORES ALL RESULTS. THE COUNTER IS INCREMENTED AND THE WORD IS SWAPPED AGAIN,
261 REM RESTORING THE ORIGINAL ORDER. LINE 290 THEN "ROTATES" THE WORD (ABC BECOMES CBA), THE RESULT IS STORED, THE COUNTER INCREMENTED, AND THE PROCEDURE REPEATED. 3 PASSES THROUGH THIS LOOP WILL PRODUCE ALL 6 PERMUTATIONS OF
262 REM 3 LETTERS. THESE PERMUTATIONS ARE THEN RETURNED TO THE 4-LETTER LOOP.
300 PRINT : PRINT : INPUT "HIT RETURN TO SEE MORE...";R$: PRINT
310 IF R$ = "" THEN RETURN
320 GOTO 310
324 REM
325 REM **4-LETTER LOOP**
326 REM
330 FOR J = 0 TO 3
340 X$ = LEFT$(FOUR$,1):THREE$ = RIGHT$(FOUR$,3): REM GRAB THE FIRST LETTER
350 GOSUB 230: REM SEND WHAT'S LEFT TO THE 3-LETTER LOOP
360 FOR K = 1 +6 *J +24 *M +120 *A TO 6 +6 *J +24 *M +120 *A: REM A COUNTS THE NUMBER OF PASSES THROUGH THE 6-LOOP, M COUNTS THE 5-LOOP AND J COUNTS THE 4-LOOP - EACH PERMUTATION GETS A UNIQUE PLACE IN THE ARRAY A$()
370 A$(K) = X$ +A$(K)
380 NEXT K
390 FOUR$ = RIGHT$(FOUR$,1) + LEFT$(FOUR$,3): REM MAKE A NEW WORD BY TAKING THE LAST LETTER AND MAKING IT FIRST (ABCD BECOMES DABC)
400 NEXT J: REM DO IT ALL AGAIN WITH THE NEW 4-LETTER WORD
410 RETURN
414 REM
415 REM **5-LETTER LOOP**
416 REM
420 FOR M = 0 TO 4
430 W$ = LEFT$(FIVE$,1):FOUR$ = RIGHT$(FIVE$,4): REM GRAB THE FIRST LETTER
440 GOSUB 330: REM SEND WHAT'S LEFT TO THE 4-LOOP
450 FOR N = 1 +24 *M +120 *A TO 24 +24 *M +120 *A
460 A$(N) = W$ +A$(N)
470 NEXT N
480 FIVE$ = RIGHT$(FIVE$,1) + LEFT$(FIVE$,4): REM MAKE A NEW 5-LETTER WORD BY PUTTING THE LAST LETTER FIRST (ABCDE BECOMES EABCD)
490 NEXT M: REM DO IT ALL AGAIN WITH THE NEW 5-LETTER WORD